package com.cidp.system.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;
import org.extremecomponents.table.limit.Limit;

import com.cidp.system.model.Tpermission;
import com.cidp.system.model.Trole;
import com.cidp.system.pojo.PermissionPo;
import com.cidp.system.pojo.RolePo;
import com.cidp.system.service.IPermissionService;
import com.litt.core.common.Constants;
import com.litt.core.page.IPageList;
import com.litt.core.page.PagePoParam;
import com.litt.core.util.ExtremeTableUtil;
/**
 * <b>标题：</b>权限管理action类.
 * <pre><b>描述</b>
 *    操作员管理
 * </pre>
 * 
 * @author zhanglei 
 * @since 2008-6-6
 * @version 1.0
 */
public class PermissionAction  extends DispatchAction{
	private IPermissionService permissionService;
	/**
	 * 查询
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward search(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		PermissionPo permissionPo=(PermissionPo)dynaActionForm.get("permissionPo");
		Limit limit=ExtremeTableUtil.getLimit(request,Constants.DEFAULT_PAGE_SIZE);
		IPageList page = permissionService.listByPage(new PagePoParam(permissionPo, limit.getPage(), limit.getCurrentRowsDisplayed()));
		request.setAttribute("resultList", page.getRsList());
		request.setAttribute("totalRows", new Integer(page.getTotalSize()));
		return mapping.findForward("permissionList");
	}
	/**
	 * 增加
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward add(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		return mapping.findForward("permissionAdd");
	}
	/**
	 * 保存
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward save(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		PermissionPo permissionPo=(PermissionPo)dynaActionForm.get("permissionPo");
		Tpermission permission=new Tpermission();
		permission.setPermissionCode(permissionPo.getPermissionCode());
		permission.setPermissionName(permissionPo.getPermissionName());
		permission.setRemark(permissionPo.getRemark());
		permissionService.save(permission);
		return mapping.findForward("success");
	}
	/**
	 * 編輯
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward edit(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		PermissionPo permissionPo=(PermissionPo)dynaActionForm.get("permissionPo");
		String sId=request.getParameter("id");
		Long id=new Long(sId);
		Tpermission permission=permissionService.load(id);
		BeanUtils.copyProperties(permissionPo,permission);
		dynaActionForm.set("permissionPo", permissionPo);
		return mapping.findForward("permissionEdit");
	}
	/**
	 * 修改
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward update(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DynaActionForm dynaActionForm=(DynaActionForm) form;
		PermissionPo permissionPo=(PermissionPo)dynaActionForm.get("permissionPo");
		Tpermission permission=permissionService.load(permissionPo.getSerialNo());
		permission.setPermissionCode(permissionPo.getPermissionCode());
		permission.setPermissionName(permissionPo.getPermissionName());
		permission.setRemark(permissionPo.getRemark());
		permissionService.update(permission);
		return mapping.findForward("success");
	}
	public void setPermissionService(IPermissionService permissionService) {
		this.permissionService = permissionService;
	}
}
